﻿using System;
using System.Collections.Generic;

namespace Mono
{
	// Token: 0x0200000A RID: 10
	internal class MergeSort<T>
	{
		// Token: 0x0600001F RID: 31 RVA: 0x0000294D File Offset: 0x00000B4D
		private MergeSort(T[] elements, IComparer<T> comparer)
		{
			this.elements = elements;
			this.buffer = new T[elements.Length];
			Array.Copy(this.elements, this.buffer, elements.Length);
			this.comparer = comparer;
		}

		// Token: 0x06000020 RID: 32 RVA: 0x00002988 File Offset: 0x00000B88
		public static void Sort(T[] source, IComparer<T> comparer)
		{
			MergeSort<T>.Sort(source, 0, source.Length, comparer);
		}

		// Token: 0x06000021 RID: 33 RVA: 0x00002997 File Offset: 0x00000B97
		public static void Sort(T[] source, int start, int length, IComparer<T> comparer)
		{
			new MergeSort<T>(source, comparer).Sort(start, length);
		}

		// Token: 0x06000022 RID: 34 RVA: 0x000029A9 File Offset: 0x00000BA9
		private void Sort(int start, int length)
		{
			this.TopDownSplitMerge(this.buffer, this.elements, start, length);
		}

		// Token: 0x06000023 RID: 35 RVA: 0x000093E4 File Offset: 0x000075E4
		private void TopDownSplitMerge(T[] a, T[] b, int start, int end)
		{
			bool flag = end - start < 2;
			if (!flag)
			{
				int num = (end + start) / 2;
				this.TopDownSplitMerge(b, a, start, num);
				this.TopDownSplitMerge(b, a, num, end);
				this.TopDownMerge(a, b, start, num, end);
			}
		}

		// Token: 0x06000024 RID: 36 RVA: 0x0000942C File Offset: 0x0000762C
		private void TopDownMerge(T[] a, T[] b, int start, int middle, int end)
		{
			int num = start;
			int num2 = middle;
			for (int i = start; i < end; i++)
			{
				bool flag = num < middle && (num2 >= end || this.comparer.Compare(a[num], a[num2]) <= 0);
				if (flag)
				{
					b[i] = a[num++];
				}
				else
				{
					b[i] = a[num2++];
				}
			}
		}

		// Token: 0x04000008 RID: 8
		private readonly T[] elements;

		// Token: 0x04000009 RID: 9
		private readonly T[] buffer;

		// Token: 0x0400000A RID: 10
		private readonly IComparer<T> comparer;
	}
}
